﻿@using System.Text.RegularExpressions
@model IEnumerable<KRM.Core.KnowledgeBase>
@{
    ViewBag.Title = "Knowledge base";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var page = 1;

    if (Request.QueryString["p"] != null)
    {
        page = Convert.ToInt32(Request.QueryString["p"]);

    }

    int itemPerPage = 15;
    int pagingCount = Convert.ToInt32(Math.Ceiling((Model.Count() / (float)itemPerPage)));
    var listPaging = Model.Skip((page - 1) * itemPerPage).Take(itemPerPage).ToList();
}

<div class="container-fluid">
    <div class="box">
        <div class="box-title">
            <h3><i class="icon-tasks"></i>Knowledge base</h3>
        </div>

        <div class="box-content">
            <form class="margin0" action="/knowledgebase" method="get">
                <div class="row-fluid">
                    <div class="span3">
                        <a href="/knowledgebase/item/0" class="btn btn-success" style="margin-bottom: 20px"><i class="icon-edit"></i>Add new Knowledge base</a>
                    </div>
                    <div class="span5"></div>
                    <div class="span3">
                        <input type="text" name="q" required="required" class="input-block-level" placeholder="search..." />
                    </div>
                    <div class="span1">
                        <button type="submit" class="btn btn-primary" style="width: 100%">Search</button>
                    </div>
                </div>
            </form>
            @Html.Partial("Partial/_MessageResult")
            <table class="table table-hover table-bordered table-fix">
                <thead>
                    <tr>
                        <th style="text-align: center">ID</th>
                        <th>Title</th>
                        <th>Content</th>
                        <th>Create Date</th>
                        <th style="text-align: center">Action</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in listPaging)
                    {
                        var content = Regex.Replace(item.Content, "<.*?>", string.Empty);
                        <tr>
                            <td style="text-align: center">@item.Id</td>
                            <td>@item.Title</td>
                            <td>@content</td>
                            <td>@item.CreateDate</td>
                            <td style="text-align: center">
                                <a href="/knowledgebase/view/@item.Id" title="Edit: @item.Title" class="btn btn-primary">View</a>
                                <a href="/knowledgebase/deleteitem/@item.Id" title="Delete: @item.Title" class="btn btn-danger" 
                                   onclick="return confirm('Are you sure?')">Delete</a>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
            @if (pagingCount > 1)
            {
                <div class="paging">
                    <a href="/knowledgebase">«</a>
                    <span>
                        @for (int j = page - 3; j <= page + 3; j++)
                        {
                            if (j > 0 && j <= pagingCount)
                            {
                                if (j == page)
                                {
                            <a href="/knowledgebase?p=@j" class="page-active">@j</a>
                                }
                                else
                                {
                            <a href="/knowledgebase?p=@j">@j</a>
                                }

                            }
                        }
                    </span>
                    <a href="/knowledgebase?p=@pagingCount">»</a>
                </div>
            }
        </div>
    </div>
</div>
